// -----------------------------------------------
// Author:     code34 nicolas_boiteux@yahoo.fr
// Edited by:  Neumatic
// Warcontext: Create a recruitment dialog box
// -----------------------------------------------

private ["_maxsize","_name","_index","_unit","_type","_textbox","_array"];

if !(isFormationLeader player) exitWith {
	wcgarbage = [localize "STR_WC_MENURECRUITMENT", "You are not leader of your group", "Create a new group", 10] spawn WC_fnc_playerhint;
	closeDialog 0;
};

if !(name player in wcinteam) exitWith {
	wcgarbage = [localize "STR_WC_MENURECRUITMENT", "Only members of team can recruit", "Wait to be recruit as team member", 10] spawn WC_fnc_playerhint;
	closeDialog 0;
};

if (wcrecruitberanked == 1) then {
	switch (rank player) do {
		case "Private": {    _maxsize = 1 };
		case "Corporal": {   _maxsize = 2 };
		case "Sergeant": {   _maxsize = 3 };
		case "Lieutenant": { _maxsize = 4 };
		case "Captain": {    _maxsize = 5 };
		case "Major": {      _maxsize = 6 };
		case "Colonel": {    _maxsize = 7 };
	};
} else {
	_maxsize = 64;
};

disableSerialization;
_textbox = (uiNamespace getVariable "wcdisplay") displayCtrl 4001;

_array = [];
{
	if ((_x select 0) == faction player) then {
		_name = getText (configFile >> "CfgVehicles" >> (_x select 1) >> "DisplayName");
		_index = lbAdd [4002, _name];
		_array set [_index, (_x select 1)];
	};
} foreach (wcwestside select 1);
lbSetCurSel [4002, 0];

ctrlSetText [4005, format ["Recruitment avalaible: %1 soldiers", (_maxsize - count (units (group player)))]];

while {alive player && {dialog}} do {
	_index = lbCurSel 4002;
	_type = _array select _index;

	if (menuaction == 1) then {
		menuaction = -1;

		if (count (units (group player)) < _maxsize) then {
			_unit = (group player) createUnit [_type, position player, [], 0, "FORM"];
			wcgarbage = [localize "STR_WC_MENURECRUITMENT", "Manage your squad", format["%1 joined your squad", name _unit], 2] spawn WC_fnc_playerhint;
			[_unit] joinSilent (group player);
			_unit addEventHandler ["Killed", {
				wcgarbage = _this spawn WC_fnc_garbagecollector;
				wcaddkilled = _unit;
				["wcaddkilled", "server"] call WC_fnc_publicvariable;
			}];
			if (_type in wcmedicclass) then {
				wcgarbage = [_unit] spawn WC_fnc_createmedic;
			};
		} else {
			if (random 1 > 0.02) then {
				wcgarbage = [localize "STR_WC_MENURECRUITMENT", "You are not enough ranked to give some orders", "Increase your rank by winning points", 10] spawn WC_fnc_playerhint;
			} else {
				wcgarbage = [localize "STR_WC_MENURECRUITMENT", "Ok, recruiter give you one men because you are a friend", "Increase your rank by winning points", 10] spawn WC_fnc_playerhint;
				sleep 10;
				_unit = (group player) createUnit [_type, position player, [], 0, "FORM"]; 
				[_unit] joinSilent (group player);
				_unit addEventHandler ["killed", {
					wcgarbage = _this spawn WC_fnc_garbagecollector;
					wcaddkilled = _unit;
					["wcaddkilled", "server"] call WC_fnc_publicvariable;
				}];
				if (_type in wcmedicclass) then {
					wcgarbage = [_unit] spawn WC_fnc_createmedic;
				};
			};
		};

		ctrlSetText [4005, format ["Recruitment avalaible: %1 soldiers", (_maxsize - count (units (group player)))]];
	};

	if (menuaction == 2) then {
		menuaction = -1;
		closeDialog 0;
	};

	sleep 0.5;
};